//
//  AYSLiveViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/27.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSLiveViewController.h"
#import "AYSLiveTableViewCell.h"
#import "AYSLiveModel.h"
#import <UIImageView+WebCache.h>
#import "AYSRequestManager.h"
#import "AYSCommonPlayViewController.h"
#import "AYSVideoPlayerViewController.h"
@interface AYSLiveViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *mainTableView;
@property(nonatomic, strong)NSMutableArray *liveArr;
@property(nonatomic, strong)UIView *topView;
@property(nonatomic, strong)UILabel *titleLable;
@end

@implementation AYSLiveViewController
- (void)requestDataWithLive
{
    [AYSRequestManager requestWithType:GET url:KLive parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        NSArray *channelsArr = dataDic[@"channels"];
        for (NSDictionary *resultDic in channelsArr)
        {
            AYSLiveModel *model = [[AYSLiveModel alloc] init];
            [model setValuesForKeysWithDictionary:resultDic];
            [_liveArr addObject:model];
        }
        [self getMainTableView];
        [_mainTableView reloadData];
       
        
    } error:^(NSError *error)
     {
         NSLog(@"更多动漫网络请求失败");
     }];
    
    
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
//    self.navigationItem.title = @"直播";
    self.navigationController.navigationBar.hidden = YES;

    _liveArr = [NSMutableArray arrayWithCapacity:0];
    [self requestDataWithLive];
    [self topView];
   
//    UIColor *color = [UIColor whiteColor];
//    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
//    self.navigationController.navigationBar.titleTextAttributes = dict;
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.032 alpha:1.000];
    


}

- (UIView *)topView
{
    if (!_topView)
    {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 50)];
        _topView.backgroundColor = [UIColor blackColor];
        
        _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 10, 8, 100, 30)];
        _titleLable.textColor = [UIColor whiteColor];
        _titleLable.text = @"直播";
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 45, SCREEN_WIDTH, 1)];
        view.backgroundColor = [UIColor lightGrayColor];
        
        [self.topView addSubview:view];
        [self.topView addSubview:_titleLable];
        [self.view addSubview:_topView];
    }
    return _topView;
}


- (void)getMainTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 71, SCREEN_WIDTH, SCREEN_HEIGHT - 130)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 80;
    _mainTableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_mainTableView];
    
    // 注册
    [_mainTableView registerClass:[AYSLiveTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

#pragma wark ======================配置数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _liveArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSLiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor blackColor];
    AYSLiveModel *model = _liveArr[indexPath.row];
    cell.model = model;
    if (model.current_programs.title != nil)
    {
        cell.markImage.image = [UIImage imageNamed:@"感叹号.png"];
        cell.playImage.image= [UIImage imageNamed:@"播放1.png"];
    }
    else
    {
        cell.markImage.image = nil;
        cell.playImage.image = nil;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   AYSVideoPlayerViewController *playVC = [[AYSVideoPlayerViewController alloc] init];
     AYSLiveModel *model = _liveArr[indexPath.row];
//    playVC.url = model.current_programs.web_url;
    playVC.url = @"http://v.qq.com/live/tv/28.html";
    NSLog(@"web_url %@", model.current_programs.web_url);
    [self presentViewController:playVC animated:YES completion:nil];
}


#pragma wark ================添加每个cell出现时的动画（从左边出现）
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D rotation;// 3D旋转
    rotation.m34 = 1.0 / -600;// 逆时针旋转
    
    
    cell.layer.shadowColor = [[UIColor blackColor] CGColor];
    cell.contentView.layer.shadowOffset = CGSizeMake(0, 0);
    cell.alpha = 0;
    
    cell.contentView.layer.transform = rotation;
    cell.contentView.layer.anchorPoint = CGPointMake(0, 0.5);// 旋转定点
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.8];
    cell.contentView.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.contentView.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
